﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Xml;
using System.Text;

public partial class Whir_System_Module_CommonModel_TableChangeModel : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridView1.DataSource = GetTableInfo(DropDownList1.SelectedItem.Value);
        GridView1.DataBind();
    }

    /// <summary>
    /// 绑定数据
    /// </summary>
    private void BindData()
    {
        DataTable dt = Whir.SQLServerDAL.DbHelperSQL.GetDataTable("[Name]", "SysObjects", "XType='U'", "[Name]");
        DropDownList1.DataSource = dt;
        DropDownList1.DataTextField = "Name";
        DropDownList1.DataValueField = "Name";
        DropDownList1.DataBind();
    }

    /// <summary>
    /// 获取指定数据表的信息
    /// </summary>
    /// <param name="tableName">表名</param>
    /// <returns>返回 DataTable 类型的数据表信息</returns>
    public DataSet GetTableInfo(string tableName)
    {
        SqlParameter[] parameters = {
					new SqlParameter("@tablename", SqlDbType.VarChar,32)
            };
        parameters[0].Value = tableName;

        return Whir.SQLServerDAL.DbHelperSQL.RunProcedure("Proc_TableInfo", parameters, "ds");
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string modelMark = txtModelMark.Text;
        string modelName = txtModelName.Text;
        string modeldesc = txtModelDesc.Text;
        string modelIcon = txtModelIcon.Text;
        string tableName = this.DropDownList1.SelectedItem.Value;
        string isUse = txtIsUse.Text;
        DataTable selectTableInfo = GetTableInfo(tableName).Tables[0];

        bool flag = CheckFieldDesc(selectTableInfo);
        if (!flag)
        {
            Page.ClientScript.RegisterClientScriptBlock(typeof(Whir_System_Module_CommonModel_TableChangeModel), "",
                "<script>alert('此表中除标识、主键字段外，其他字段必须填写字段说明！');</script>");
            return;
        }
        string fileContentType = ".xml";
        string filePath = HttpContext.Current.Server.MapPath("~/Temp/");
        XmlTextWriter w = new XmlTextWriter(filePath + tableName + fileContentType, Encoding.UTF8);
        w.Formatting = Formatting.Indented;

        #region 文档开始
        w.WriteStartDocument();

        #region Model开始
        w.WriteStartElement("Model");

        w.WriteStartElement("ModelMark");
        w.WriteString(modelMark);
        w.WriteEndElement();

        w.WriteStartElement("ModelName");
        w.WriteString(modelName);
        w.WriteEndElement();

        w.WriteStartElement("ModelDesc");
        w.WriteString(modeldesc);
        w.WriteEndElement();

        w.WriteStartElement("ModelIcon");
        w.WriteString(modelIcon);
        w.WriteEndElement();

        w.WriteStartElement("TableName");
        w.WriteString(tableName);
        w.WriteEndElement();

        w.WriteStartElement("IsUse");
        w.WriteString(isUse);
        w.WriteEndElement();

        w.WriteStartElement("Fields");
        foreach (DataRow dr in selectTableInfo.Rows)
        {
            if (dr["主键"].ToString() == "×")
            {
                w.WriteStartElement("Field");

                w.WriteAttributeString("fieldName", dr["字段名"].ToString());
                w.WriteAttributeString("alias", dr["字段名"].ToString());
                w.WriteAttributeString("rating", "true");
                w.WriteAttributeString("fieldTip", "");

                w.WriteAttributeString("fieldDesc", "");
                w.WriteAttributeString("isRequired", dr["允许空"].ToString() == "√" ? "True" : "False");
                w.WriteAttributeString("isUse", "true");
                w.WriteAttributeString("sort", dr["字段序号"].ToString());

                w.WriteAttributeString("fieldType", ((int)GetFieldType(dr["字段说明"].ToString())).ToString());
                w.WriteAttributeString("typeName", dr["类型"].ToString());
                w.WriteAttributeString("typeLength", dr["占用字节数"].ToString());
                w.WriteAttributeString("isAllowNull", dr["允许空"].ToString() == "√" ? "False" : "True");

                #region 字段信息
                switch (GetFieldType(dr["字段说明"].ToString()))
                {
                    case FieldType.None:
                    case FieldType.TextType:
                    case FieldType.MoneyType:
                    case FieldType.ColorType:
                    case FieldType.NumberType:
                    case FieldType.LinkType:
                        w.WriteAttributeString("textWidth", "400");
                        w.WriteAttributeString("textHeight", "");
                        w.WriteAttributeString("textType", "1");
                        w.WriteAttributeString("defaultValue", "");
                        break;
                    case FieldType.MultipleTextType:
                        w.WriteAttributeString("textWidth", "400");
                        w.WriteAttributeString("textHeight", "80");
                        w.WriteAttributeString("textType", "2");
                        w.WriteAttributeString("defaultValue", "");
                        break;
                    case FieldType.DateTimeType:
                        w.WriteAttributeString("dataFormat", "0");
                        w.WriteAttributeString("width", "200");
                        w.WriteAttributeString("defaultValue", "1");
                        break;
                    case FieldType.BoolType:
                        w.WriteAttributeString("optionType", "2");
                        w.WriteAttributeString("bindValue", "是|否");
                        w.WriteAttributeString("cellNum", "0");
                        w.WriteAttributeString("bindType", "否");
                        w.WriteAttributeString("defaultValue", "否");
                        break;
                    case FieldType.ListBoxType:
                        w.WriteAttributeString("optionType", "1");
                        w.WriteAttributeString("bindValue", "");
                        w.WriteAttributeString("cellNum", "0");
                        w.WriteAttributeString("bindType", "");
                        w.WriteAttributeString("defaultValue", "");
                        break;
                    case FieldType.MultipleHtmlTextType:
                        w.WriteAttributeString("editorMode", "1");
                        w.WriteAttributeString("editorType", "1");
                        w.WriteAttributeString("textWidth", "600");
                        w.WriteAttributeString("textHeight", "350");
                        w.WriteAttributeString("defaultValue", "");
                        break;
                    case FieldType.FileType:
                        w.WriteAttributeString("maxSize", "4000");
                        w.WriteAttributeString("allowType", "gif|jpg|jpeg|bmp|pdf|doc|xls|zip|rar|7z");
                        w.WriteAttributeString("defaultValue", "");
                        break;
                    case FieldType.PictureType:
                        w.WriteAttributeString("maxSize", "2048");
                        w.WriteAttributeString("allowType", "gif|jpg|jpeg|png|bmp");
                        w.WriteAttributeString("defaultValue", "");
                        break;
                }
                #endregion
                w.WriteEndElement();
            }
        }

        w.WriteEndElement();


        w.WriteEndElement();
        #endregion

        w.WriteEndDocument();
        #endregion

        w.Flush();
        w.Close();
        Page.ClientScript.RegisterClientScriptBlock(typeof(Whir_System_Module_CommonModel_TableChangeModel), "",
    "<script>alert('生成成功');</script>");
    }

    protected bool CheckFieldDesc(DataTable dt)
    {
        bool flag = true;
        foreach (DataRow dr in dt.Rows)
        {
            if (dr["标识"].ToString() == "×"
                && dr["主键"].ToString() == "×"
                && string.IsNullOrEmpty(dr["字段说明"].ToString()))
            {
                flag = false;
                break;
            }
        }
        return flag;
    }

    protected FieldType GetFieldType(string fieldType)
    {
        switch (fieldType)
        {
            case "BoolType":
                return FieldType.BoolType;
            case "ColorType":
                return FieldType.ColorType;
            case "DateTimeType":
                return FieldType.DateTimeType;
            case "FileType":
                return FieldType.FileType;
            case "LinkType":
                return FieldType.LinkType;
            case "ListBoxType":
                return FieldType.ListBoxType;
            case "MoneyType":
                return FieldType.MoneyType;
            case "MultipleHtmlTextType":
                return FieldType.MultipleHtmlTextType;
            case "MultipleTextType":
                return FieldType.MultipleTextType;
            case "None":
                return FieldType.None;
            case "NumberType":
                return FieldType.NumberType;
            case "PictureType":
                return FieldType.PictureType;
            case "TextType":
                return FieldType.TextType;
            default:
                return FieldType.TextType;
        }
    }
}
